﻿var ns = namespace("kaskelot.sp.ui");

ns.byggnadmateriel = function () {

	function getList() {
		var id = getParameterByName('ByggnadId');

		$(".blockable-container").block({ message: null });

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/GetMaterielListByByggnad",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
                function (data) {
					if (!data.GetMaterielListByByggnadResult.HasError) {
						$("#materiel-list tbody").html(
							$("#materiel-template").render(data.GetMaterielListByByggnadResult.Data)
						);
					}
					$(".blockable-container").unblock();
                },
			error:
                function (xhr) {
					$(".blockable-container").unblock();
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showEditForm(id) {
		var byggnadId = getParameterByName('ByggnadId');
		var materielId = id;
		if (materielId === undefined)
			materielId = "";

		var selector = "#materiel-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "MaterielForm",
				idString: materielId,
				parentIdString: byggnadId
			},
			selector: selector,
			openFunction: function () {
				$("#Antal").spinner().spinner('option', 'min', 1);
				$(".chzn-select-deselect").chosen({ disable_search: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "";
				if (materielId === "")
					method = "CreateMateriel";
				else
					method = "UpdateMateriel";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					cache: false,
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							getList();
							$(selector).dialog("close");
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showDeleteForm(id) {
		var byggnadId = getParameterByName('ByggnadId');
		var materielId = id;
		if (materielId === undefined)
			materielId = "";

		var selector = "#materiel-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "MaterielForm",
				idString: materielId,
				parentIdString: byggnadId
			},
			selector: selector,
			openFunction: function () {
				$("#materiel-form #MaterielTypId").attr("disabled", "disabled");
				$("#materiel-form #Antal").attr("disabled", "disabled");
				$("#materiel-form #toggle-btn").attr("disabled", "disabled");

				$("#materiel-form #Antal").spinner().spinner('option', 'min', 1).spinner("disable");
				$("#materiel-form .chzn-select-deselect").chosen({ disable_search: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "DeleteMateriel";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientByggnadService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					cache: false,
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							getList();
							$(selector).dialog("close");
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	return {
		getList: getList,
		showEditForm: showEditForm,
		showDeleteForm: showDeleteForm
	};
}();